﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lenovo.CFI.BLL;
using Lenovo.CFI.BLL.DicMgr;
using Lenovo.CFI.Common;
using Lenovo.CFI.Web.Helper;

namespace Lenovo.CFI.Web.VP
{
    public partial class ApproverList : System.Web.UI.UserControl
    {
        IdeaBl ideaBl = new IdeaBl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindHelper.BindTopics(DdlTopic, "Select...", "-1", null);

                ViewState["Topic"] = this.DdlTopic.SelectedValue;
                ViewState["Status"] = this.DdlStatus.SelectedValue;

                this.BindData();

                MessageHelper.Prepare(this.Page);
            }
        }

        protected void DdlTopic_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["Topic"] = this.DdlTopic.SelectedValue;

            this.BindData();
        }

        protected void DdlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["Status"] = this.DdlStatus.SelectedValue;

            this.BindData();
        }



        private void BindData()
        {
            int? topicID = null;
            if (ViewState["Topic"] != null) topicID = Convert.ToInt32(ViewState["Topic"]);
            if (topicID == -1) topicID = null;

            IdeaStatus? status = null;
            if (ViewState["Status"] != null)
                status = (IdeaStatus)Convert.ToInt32(ViewState["Status"]);

            GvList.DataSource = ideaBl.GetIdeaForApprover(UserHelper.UserName, topicID, status);
            GvList.DataBind();
        }

        protected void GvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // 设置行号
                ((Literal)(e.Row.FindControl("LtrNo"))).Text = String.Format("{0}", e.Row.DataItemIndex + 1);


                Idea idea = (Idea)e.Row.DataItem;

                ((Literal)e.Row.FindControl("LtrName")).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("ideadetail", "id", idea.ID.ToString()), idea.Title);
                ((Literal)e.Row.FindControl("LtrType")).Text = IdeaTypeDicMgr.GetByCode(idea.Type).Title;
                ((Literal)e.Row.FindControl("LtrOwner")).Text = idea.Owner;


                ImageButton btnAgree = (ImageButton)e.Row.FindControl("BtnAgree");
                ImageButton btnReject = (ImageButton)e.Row.FindControl("BtnReject");

                if (idea.Status != IdeaStatus.Approving)
                {
                    btnAgree.Visible = false;
                    btnReject.Visible = false;
                }
            }
        }


        protected void GvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Agree")
            {
                ViewState["IdeaID"] = e.CommandArgument;
                ViewState["Agree"] = 1;

                this.LtrCaption.Text = "Agree";
            }
            else if (e.CommandName == "Reject")
            {
                ViewState["IdeaID"] = e.CommandArgument;
                ViewState["Agree"] = 0;

                this.LtrCaption.Text = "Reject";
            }

            this.MpeApprove.Show();
        }

        protected void GvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GvList.PageIndex = e.NewPageIndex;
            this.BindData();
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                ideaBl.Approve(Convert.ToInt32(ViewState["IdeaID"]), ((int)ViewState["Agree"] == 1), this.TxtComment.Text);

                MessageHelper.RegShowJSAjax(this.BtnSave, "Success!", this.Page);
            }
            catch (ApplicationException ex)
            {
                MessageHelper.RegShowJSAjax(this.BtnSave, ex.Message, this.Page);
            }

            this.BindData();
        }
    }
}